<?php

namespace App\Model\Help;

use App\Model\BizModel;
use App\Model\BrandModel;
use App\Model\CompanyModel;
use App\Model\DepModel;
use App\Model\ShopModel;
use App\Model\UserModel;
use Trois\Lib\Help;

class UserManage
{
    // 用户
    public array $user = [];

    // 是否管理所有事业部(*表示所有)
    public bool $isManageAllBiz = false;

    // 是否管理所有品牌(*表示所有)
    public bool $isManageAllBrand = false;

    // 是否管理所有公司(*表示所有)
    public bool $isManageAllCompany = false;

    // 是否管理所有门店(*表示所有)
    public bool $isManageAllShop = false;

    // 是否管理所有部门(*表示所有)
    public bool $isManageAllDept = false;


    // 管理的事业部id集合
    public array $manageBizIds = [];

    // 管理的品牌id集合
    public array $manageBrandIds = [];

    // 管理的公司id集合
    public array $manageCompanyIds = [];

    // 管理的门店id集合
    public array $manageShopIds = [];

    // 管理的部门id集合
    public array $manageDeptIds = [];

    // 管理的事业部
    public array $manageBiz = [];

    // 管理的品牌
    public array $manageBrand = [];

    // 管理的公司
    public array $manageCompany = [];

    // 管理的门店
    public array $manageShop = [];

    // 管理的部门
    public array $manageDept = [];

    // 管理的部门 - 树型
    public array $manageDeptTree = [];

    // 管理的部门 - 员工
    public array $manageBizDeptEmployee = [];


    public function __construct(array $user)
    {
        $this->user = $user;
    }

    private function handleBiz()
    {

        if ($this->user['manage_biz_id'] === '*') {
            $this->isManageAllBiz = true;
        }

        $data = Help::modelInvoke(BizModel::class, function (BizModel $model) {
            return $model->getAllCache($this->user['group_id']);
        }, false);

        if ($data) {

            if ($this->isManageAllBiz) {

                $this->manageBiz = $data;

                foreach ($data as $b) {
                    $this->manageBizIds[] = $b['id'];
                }


            } else {

                $manageBizArr = explode(',', $this->user['manage_biz_id']);

                foreach ($data as $b) {

                    if (in_array($b['id'], $manageBizArr)) {
                        $this->manageBizIds[] = $b['id'];
                        $this->manageBiz[] = $b;
                    }

                }

            }

        }
    }

    private function handleBrand()
    {

        if ($this->user['bra_ids'] === '*') {
            $this->isManageAllBrand = true;
        }

        $data = Help::modelInvoke(BrandModel::class, function (BrandModel $model) {
            return $model->getAllCache($this->user['group_id']);
        }, false);

        if ($data) {

            if ($this->isManageAllBrand) {

                $this->manageBrand = $data;

                foreach ($data as $b) {
                    $this->manageBrandIds[] = $b['id'];
                }


            } else {

                $manageBizArr = explode(',', $this->user['bra_ids']);

                foreach ($data as $b) {

                    if (in_array($b['id'], $manageBizArr)) {
                        $this->manageBrandIds[] = $b['id'];
                        $this->manageBrand[] = $b;
                    }

                }

            }

        }
    }

    private function handleCompany()
    {

        if ($this->user['manage_subsidiary_id'] === '*') {
            $this->isManageAllCompany = true;
        }

        $data = Help::modelInvoke(CompanyModel::class, function (CompanyModel $model) {
            return $model->getAllCache($this->user['group_id']);
        }, false);

        if ($data) {

            if ($this->isManageAllCompany) {

                $this->manageCompany = $data;

                foreach ($data as $b) {
                    $this->manageCompanyIds[] = $b['id'];
                }


            } else {

                $manageBizArr = explode(',', $this->user['manage_subsidiary_id']);

                foreach ($data as $b) {

                    if (in_array($b['id'], $manageBizArr)) {
                        $this->manageCompanyIds[] = $b['id'];
                        $this->manageCompany[] = $b;
                    }

                }

            }

        }
    }

    private function handleShop()
    {

        if ($this->user['manage_dept_id'] === '*') {
            $this->isManageAllShop = true;
        }

        $data = Help::modelInvoke(ShopModel::class, function (ShopModel $model) {
            return $model->getAllCache($this->user['group_id']);
        }, false);

        if ($data) {

            if ($this->isManageAllShop) {

                $this->manageShop = $data;

                foreach ($data as $b) {
                    $this->manageShopIds[] = $b['id'];
                }


            } else {

                $manageBizArr = explode(',', $this->user['manage_dept_id']);

                foreach ($data as $b) {

                    if (in_array($b['id'], $manageBizArr)) {
                        $this->manageShopIds[] = $b['id'];
                        $this->manageShop[] = $b;
                    }

                }

            }

        }
    }

    private function handleDept()
    {

        if ($this->user['manage_section_id'] === '*') {
            $this->isManageAllDept = true;
        }

        $data = Help::modelInvoke(DepModel::class, function (DepModel $model) {
            return $model->getAllCache($this->user['group_id']);
        }, false);

        if ($data) {

            if ($this->isManageAllDept) {

                $this->manageDept = $data;

                foreach ($data as $b) {
                    $this->manageDeptIds[] = $b['id'];
                }


            } else {

                $manageBizArr = explode(',', $this->user['manage_section_id']);

                foreach ($data as $b) {

                    if (in_array($b['id'], $manageBizArr)) {
                        $this->manageDeptIds[] = $b['id'];
                        $this->manageDept[] = $b;
                    }

                }

            }

            $bizDeptArr = [];
            foreach ($this->manageDept as $item) {
                $bizDeptArr[$item['biz_id']][] = $item;
            }

            foreach ($bizDeptArr as $bizId => $bizDept) {
                $bizDeptArr[$bizId] = getTree($bizDept
                    , 0
                    , 'parent_id'
                    , 'id'
                    , 0
                    , 'name'
                    , 'children'
                    , []
                    , ['id'
                        , 'name'
                        , 'parent_id'
                        , 'biz_id'
                        , 'post'
                        , 'status'
                    ]);
            }

            $this->manageDeptTree = $bizDeptArr;

        }
    }

    private function handleBizBrand()
    {
        // 把通过事业部管理品牌ID过滤掉用户的管理品牌ID
        foreach ($this->manageBiz as $k => $b) {
            $bizBrand = [];

            if ($b['mange_brands']) {

                $workBrandIdsArr = array_intersect(explode(',', $b['mange_brands']), $this->manageBrandIds);

                foreach ($this->manageBrand as $brand) {
                    if (in_array($brand['id'], $workBrandIdsArr)) {
                        $bizBrand[] = $brand;
                    }
                }
            }

            $b['mange_brands'] = $bizBrand;

            $this->manageBiz[$k] = $b;

        }
    }

    private function handleDeptEmployee()
    {
        $data = Help::modelInvoke(UserModel::class, function (UserModel $model) {
            return $model->getAllCache($this->user['group_id']);
        }, false);

        if ($data) {

            $userBizDeptArr = [];

            foreach ($data as $b) {
                if (in_array($b['dept_id'], $this->manageDeptIds)) {
                    $userBizDeptArr[$b['biz_id']][$b['dept_id']][] = $b;
                }
            }

            $this->manageBizDeptEmployee = $userBizDeptArr;

        }
    }


    public function handle()
    {
        $this->handleBiz();
        $this->handleBrand();
        $this->handleCompany();
        $this->handleShop();
        $this->handleDept();
        $this->handleBizBrand();
        $this->handleDeptEmployee();

    }

}